package com.hkw.dao.impl;

import com.hkw.dao.IUserDao;
import com.hkw.model.User;
import com.hkw.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * IUserDao的实现类，负责具体的方法实现
 */
public class UserDaoImpl implements IUserDao {
    @Override
    public int findByNameAndPassword(User user) throws SQLException {
            //利用DBUtils工具类的方法更方便的操作jdbc
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            Connection con = DruidUtils.getConnection();
            String sql = "select * from user where username = ? and password =?";
            Object[] obs = new Object[]{user.getUserName(),user.getPassword()};
            User usr = qr.query(con, sql, new BeanHandler<>(User.class), obs);
            //释放连接
            con.close();
            if(usr != null){
                return 1;
            }
            return 0;
    }
}
